###############################################################################
# Appendix Figure 10 1a
###############################################################################
#
###############################################################################
# Content
###############################################################################
# 1) Dependencies
# 2) Load Data
# 3) Aggregation for Figure
# 4) Figure
###############################################################################
# 1) Dependencies
###############################################################################
library(readr)
library(dplyr)
library(plyr)
library(ggplot2)
library(gganimate)
library(ggeffects)
library(ggExtra)
library(ggridges)
library(ggrepel)
library(grid)
library(scales)
library(lubridate)
library(extrafont)
library(reshape2)
library(here)
library(ggforce)
library(png)
library(readxl)
library(grid)
library(gridExtra)
library(ggpubr)
library(ggalt)
library(stringr)
###############################################################################
# 2) Load Data
###############################################################################
# Set Path
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
rm(list=ls())

# Custom functions
# ggplot rescale x axis....
scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
# ggplot order over facets...
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

suppressWarnings(source('ggplot_theme_ddl.R', encoding = "UTF-8"))

# Load Candidates Lists (full lists not just the one we found in the newsarticles)
bfs_list_2019 <- read_delim("../support/parliament/frauenanteil_nr_zeitreihe.csv", delim = ";")
###############################################################################
# 3) Aggregations for Figure
###############################################################################
agg <- bfs_list_2019 %>% filter(kanton_bezeichnung == "CH") %>% 
  dplyr::group_by(wahl_jahr) %>% dplyr::mutate(f_kand = anzahl_kandidierende_f / anzahl_kandidierende,
                                               f_gewh = anzahl_gewaehlte_f / anzahl_gewaehlte)

agg <- agg %>% pivot_longer(cols = c(f_kand, f_gewh), names_to = "Type") %>% 
               dplyr::mutate(Type = ifelse(Type == "f_gewh", "Elected Parliamentarians", "Candidates"))
###############################################################################
# 4) Figure
###############################################################################
values_type <- c("Elected Parliamentarians" = "#DD2461", "Candidates" = "#7D7D7C")


fig_A <- ggplot(data = agg, aes(x=wahl_jahr,y=value,fill=Type, color=Type)) +
  geom_bar(stat = "identity", position = position_dodge(width = 1.9), width = 1.5) +
  labs(x = "", y = "Share of Female Candidates [%]", title = "Female Candidats over Time in the National Council", color = "Type:", fill = "Type:") +
  scale_y_continuous(labels = percent_format(scale = 100), breaks = seq(0,.5,by=.05)) +
  scale_x_continuous(breaks = c(1991,1995,1999,2003,2007,2011,2015,2019)) +
  scale_color_manual(values=values_type) +
  scale_fill_manual(values=values_type) +
  ddl_theme(type = 'default',
            panel.grid.major=element_blank(),
            legend.position='none',
            axis.line.y.left = element_line(colour="black"),
            axis.line.x.bottom = element_line(colour="black")) +
  theme(legend.position = "bottom", legend.direction = "horizontal",
        strip.background = element_blank(), strip.text = element_text(color = "black"),
        axis.text.x = element_text(angle = 0, hjust = .5, vjust = 1, size = 20),
        axis.text.y = element_text(hjust=.5, size = 20),
        strip.text.x = element_text(size = 20),
        axis.title = element_text(size = 20),
        plot.title = element_text(size = 24),
        legend.text = element_text(size = 20),
        legend.title = element_text(size = 20),
        plot.margin = unit(c(.5,1.3,.5,.5), "cm"),
        legend.key.size = unit(1.5,"line"),
        axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5),
        panel.spacing.x=unit(2.5, "lines"))

fig_A

ggsave(plot = fig_A, filename ='../img_appendix/afigure_10_1a.png',width=16, height=12, dpi = 300, bg = "white")

